<?php

namespace admin\models;

use Yii;

/**
 * This is the model class for table "mm_user_extra".
 *
 * @property integer $me_id
 * @property integer $mu_id
 * @property string $me_phone
 * @property string $me_qq
 * @property string $me_wx
 * @property string $me_livetype
 * @property string $me_postion
 * @property string $me_address
 * @property string $me_env
 *
 * @property Info $mu
 */
class UserExtra extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mm_user_extra';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['me_phone','me_qq','me_wx','me_livetype','me_env','me_address'],'required','on'=>['add','update']],
            [['mu_id'], 'integer'],
            [['me_livetype', 'me_env','me_address'], 'string'],
            [['me_phone'], 'string', 'max' => 13],
            [['me_qq', 'me_wx'], 'string', 'max' => 32],
            [['me_qq'],'checkQq','on'=>'add'],
            [['me_wx'],'checkWx','on'=>'add'],
            [['me_phone'],'validPhone','on'=>'add'],
            [['me_qq'],'checkThisQq','on'=>'update'],
            [['me_wx'],'checkThisWx','on'=>'update'],
            [['me_phone'],'validThisPhone','on'=>'update'],
            [['me_postion','me_address'], 'string', 'max' => 128],
            [['mu_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserInfo::className(), 'targetAttribute' => ['mu_id' => 'mi_id']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['add']=['me_phone','me_qq','me_wx','me_livetype','me_env','me_postion','me_address'];
        $scenarios['add']=['me_phone','me_qq','me_wx','me_livetype','me_env','me_postion','mu_id','me_address'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'me_id' => '用户其他信息id',
            'mu_id' => '用户id',
            'me_phone' => '手机',
            'me_qq' => 'QQ号',
            'me_wx' => '微信号',
            'me_livetype' => '住房性质',
            'me_postion' => '现住地址',
            'me_address'=>'详细地址',
            'me_env' => '居住环境',
        ];
    }

    public function validPhone(){
        if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9][0-9]{8}|17[0-9]{9}$|14[0-9]{9}$/",$this->me_phone) && strlen($this->me_phone)==11){
            $this->addError('ma_phone','手机格式错误');
        }elseif(!empty($this->getUserByPhone($this->me_phone))){
            $this->addError('me_phone','该手机号用户已存在');
        }
    }

    public function checkQq(){
        if(!empty($this->getUserByQq($this->me_qq))){
            $this->addError('me_qq','该QQ号用户已存在');
        }
    }
    public function checkwx(){
        if(!empty($this->getUserByWx($this->me_wx))){
            $this->addError('me_wx','该微信号用户已存在');
        }
    }

    public function validThisPhone(){
        $user = static::find()->where(['and',"me_phone='".$this->me_phone."'",'mu_id<>'.$this->mu_id])->one();
        if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9][0-9]{8}|17[0-9]{9}$|14[0-9]{9}$/",$this->me_phone) && strlen($this->me_phone)==11){
            $this->addError('ma_phone','手机格式错误');
        }elseif(!empty($user)){
            $this->addError('me_phone','该手机号用户已存在');
        }
    }

    public function checkThisQq(){
        $user = static::find()->where(['and',"me_qq='".$this->me_qq."'",'mu_id<>'.$this->mu_id])->one();
        if(!empty($user)){
            $this->addError('me_qq','该QQ号用户已存在');
        }
    }
    public function checkThiswx(){
        $user = static::find()->where(['and',"me_wx='".$this->me_wx."'",'mu_id<>'.$this->mu_id])->one();
        if(!empty($user)){
            $this->addError('me_wx','该微信号用户已存在');
        }
    }

    public static function getUserByPhone($phone){
        return self::find()->where(['me_phone'=>$phone])->one();
    }
    public static function getUserByQq($qq){
        return self::find()->where(['me_qq'=>$qq])->one();
    }
    public static function getUserByWx($wx){
        return self::find()->where(['me_wx'=>$wx])->one();
    }

    public static function Valid($tableName,$key,$value){
        $sql = "select * from $tableName WHERE $key='"."$value"."'";
        $isExist = Yii::$app->db->createCommand($sql)->queryAll();
        if(!empty($isExist)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMu()
    {
        return $this->hasOne(UserInfo::className(), ['mi_id' => 'mu_id']);
    }
}
